package org.jeecg.modules.power.statistics.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.power.device.entity.YlDevice;
import org.jeecg.modules.power.device.mapper.YlDeviceMapper;
import org.jeecg.modules.power.shop.entity.YlShop;
import org.jeecg.modules.power.shop.service.IYlShopService;
import org.jeecg.modules.power.statistics.entity.YlWeaningShopRecord;
import org.jeecg.modules.power.statistics.mapper.YlWeaningShopRecordMapper;
import org.jeecg.modules.power.statistics.service.IYlWeaningShopRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 每天撤机门店记录表
 * @Author: jeecg-boot
 * @Date:   2024-08-07
 * @Version: V1.0
 */
@Service
public class YlWeaningShopRecordServiceImpl extends ServiceImpl<YlWeaningShopRecordMapper, YlWeaningShopRecord> implements IYlWeaningShopRecordService {
    @Autowired
    private YlDeviceMapper ylDeviceMapper;//设备表

    /**
     *    APP运营端-撤机门店-2级页面         根据条件 获取相对应的 数据
     * @param type 时间类型（2昨日、3本周、4本月）
     * @return
     */
    @Override
    public List<YlWeaningShopRecord> getUnbindShopList(Integer type) {
        return baseMapper.getUnbindShopList(type);
    }

    /**
     *  APP运营端-撤机门店-2级页面- 折线图数据
     * @param groupId 小组id
     * @param bdId 组员id
     * @param dayStart 开始时间
     * @param dayEnd 结束时间
     * @return
     */
    @Override
    public List<Map<String, Object>> getUnbindShopLineChart(String groupId, String bdId, String dayStart, String dayEnd) {
        return baseMapper.getUnbindShopLineChart(groupId, bdId, dayStart, dayEnd);
    }

    @Override
    public void addWeaningShopRecordApi(YlWeaningShopRecord ylWeaningShopRecord) {

        Date date= new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        ylWeaningShopRecord.setYesterday(date);

        //首先判断 店铺 有没有设配

        //查询
        Integer p=ylDeviceMapper.selectCount(new LambdaQueryWrapper<YlDevice>().eq(YlDevice::getDeviceType,1).eq(YlDevice::getDeviceStatus,3).eq(YlDevice::getBusinessId,ylWeaningShopRecord.getShopId()));
        if (p==0){//说明此门店没有设备了
            YlWeaningShopRecord weaningShopRecord= this.getOne(new QueryWrapper<YlWeaningShopRecord>().eq("shop_id",ylWeaningShopRecord.getShopId()).eq("yesterday",dateFormat.format(date)));
            if (oConvertUtils.isNotEmpty(weaningShopRecord)){
                //更新
                weaningShopRecord.setBdId(ylWeaningShopRecord.getBdId());
                weaningShopRecord.setGroupId(ylWeaningShopRecord.getGroupId());
                weaningShopRecord.setUnbindTime(ylWeaningShopRecord.getUnbindTime());
                this.updateById(weaningShopRecord);
            }else {
                //新增
                ylWeaningShopRecord.setCreateTime(date);
                this.save(ylWeaningShopRecord);
            }
        }


    }
}
